using System.Linq;

namespace Core.Features
{
    using System;
    using Base;
    using Contracts.Enums;
    using Contracts.Interfaces;
    using Resources;

    
    public sealed class HarvesterCycleTime<T> : FeatureBase<object>
    {
        public HarvesterCycleTime(){}

        public HarvesterCycleTime(T value) 
            : base(value){ }
        /// <summary>
        /// Obtains the feature name
        /// </summary>
        public override string Name
        {
            get { return "Cycle time"; }
        }
        /// <summary>
        /// Obtains the feature group
        /// </summary>
        public override FeatureGroupsEnum FeatureGroup{ get { return FeatureGroupsEnum.Specific; } }
        /// <summary>
        /// Obtains the feature type
        /// </summary>
        public override FeaturesEnum FeatureType { get { return FeaturesEnum.HarvesterCycleTime; } }
        /// <summary>
        /// Obtains or defines the feature unit
        /// </summary>
        public override string Unit { get { return Traduction.label_unit_sec; } }

        public override void SetModifiedValue(IAgent agent, IRobot robot)
        {
            if (robot == null || agent == null) return;

            // Bonuses from extensions
            double ExtBonus = agent.Extensions.SumModifiersValues(FeatureTypesEnum.Module, this.FeatureType);

            // Bonuses from robot
            double robBonus = robot.Modifiers.SumModifiersValues(agent.Extensions, FeatureTypesEnum.Module, this.FeatureType);

            double temp = (double)Value / (1 + (ExtBonus + robBonus));

            // nexus Bonus
            double nexusBonus = robot.Modules.SumModifiersValues(FeatureTypesEnum.Module, this.FeatureType, ModuleGroupsEnum.NexusModuleFastExtractor);
            double nexusSkillBonus = agent.Extensions.SumModifiersValues(FeatureTypesEnum.Robot, this.FeatureType);

            double nexusTotalBonus = 0;

            if (nexusBonus != 0)

                nexusTotalBonus = Math.Abs(nexusBonus) + (((1 + Math.Abs(nexusBonus)) * Math.Abs(nexusSkillBonus)) / 100);

            temp = temp * (1 - nexusTotalBonus);


            ModifiedValue = IsRounded
                ? Math.Round(temp, 0, MidpointRounding.AwayFromZero)
                : Math.Round(temp, 2);
        }
    }
}